<?php

class C_Router_Regex 
{
	private $_params = array();
	private $_request = null;
	public function match($request)
	{
		
		$this->_request = $request;
		$uri = $request->getRequestUri();
		$default = get_default();
		$index = $default['module'];
		$module_array = get_module_array();
		if($this->hasHomePage($request))
		{
			$uri = '/'.$index.'/';
			
			$map = $this->loadmap($index);
			$m = $index;
				
		}
		else
		{
			preg_match("/^(?:\/\w++\.php)?\/([^\/\.]*)(.*)?/i", $uri, $module);
			if(empty($module[1]) || !in_array($module[1], $module_array))
			{
					if(empty($module[1]) || preg_match('/^\w+$/',$module[1]))
					{
						
						$map =  $this->loadmap($index);
						$m = $index;
					}
					else
					{
						return false;
					}
			}
			else
			{
				
				if(empty($module[2]) || preg_match('/(?:^\.|^\/$)/',$module[2]))
				{
					$uri = '/'.$default['method'].'/';
					
				}
				else
				{
					$uri = $module[2];
				}
				//判断模块是否是正则路由
				if(get_module_router($module[1]) != 'Regex')
				{
					return false;
				}
				
				$map =  $this->loadmap($module[1]);
		
				$m = $module[1];	
			}
			$this->_params['module'] = $m;
			return $this->getParams($uri, $map);
			
		
		}
		
	}
	private function loadmap($name)
	{
		$regex_map_path = load_regex_map($name);
		return include $regex_map_path;	
	}
	private function getParams($uri, $map)
	{
			
			if(!empty($map)&& is_array($map))
			foreach ($map as $key => $value)
			{	
				
				$dealkey = str_replace('/', '\/', $key);
				if(preg_match("/^(?:\/\w++\.php)?(?:$dealkey)($|\.|\/.*)/i",$uri,$match))
				{	
					
					 $this->_params['action'] = $map[$key]['action'];
					 $this->_params['method'] = $map[$key]['method'];
					
					if(!empty($map[$key]['param']))
					{
						$preg ='';
						$param ='';
						$param = explode(':',$map[$key]['param']);
						
						foreach($param as $key_p => $value_p)
						{
							if(!empty($value_p))
							{
								$preg .="(?:\/)?(?P<$value_p>[^\/\.\?]*)";
							}
						}
						preg_match("/^$preg/",$match[1],$match_p);
						foreach($param as $key_p => $value_p)
						{
							if(!empty($value_p))
							{
							  $this->_params[$value_p] = $match_p[$value_p]; 
							}
						}
					}
					return true;
				}
			}
			
			//都没有找到的话把参数给index
	}
	public function getRequestParams()
	{
	
		return $this->_params;
	}
	public function hasHomePage($request)
	{
		$uri = $request->getRequestUri();
		
		if($uri =='/' || preg_match("/^\/\w++\.php$/i",$uri) || preg_match("/^\/(?:\w++\.php)?(?:\?|#)/i",$uri))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function getUrl($module="",  $action,  $method, $params = array(),$query = array())
	{	
		
		$default = get_default();
		$enter_file = get_enter_file();
		if(empty($module) || $module == $default['module'])
		{
			$map = $this->loadmap($default['module']);
			$module = "";
		}
		else
		{
			$map = $this->loadmap($module);
			$module = '/'.$module;
		}
		
		foreach($map as $url => $value)
		{
			if($value['action'] == $action && $value['method'] == $method)
			{	
				
				if($module == $url)
				{
					$urlnow = "";
				}
				else
				{
					$urlnow = $url;
				}
				if($url == '/'.$default['module'])
				{
					$urlnow = "";
				}
				if(empty($enter_file))
				{
					$urlquery = $module.$urlnow.'/';
				}
				else
				{
					
					$urlquery =  '/'.$enter_file.$module.$urlnow.'/';
					
				}
			
				if(empty($value['param']))
					$param = "";
				else
					$param = $value['param'];
				if($result = $this->compactParam($param, $params))
				{
					$urlquery .= $result.'/';
				}
				if($urlquery != '/')
				{
					$urlquery = substr($urlquery, 0, -1).".do";
				}
				if(!empty($query))
				{
					$querystr = http_build_query($query);
					$urlquery .= '?'.$querystr;
				}
				return $urlquery;
				
			}
		}
		
	}
	
	
	private function compactParam($paramMap ,$params = null)
	{
		if($params ==null || !is_array($params) || empty($paramMap))
		{
			return 0;
		}
		else
		{
			extract($params);
			$param = explode(':',$paramMap);
			$result = compact($param);
			if(empty($result))
			{
				return 0;
			}
			else
			{
				  $strparam = implode('/',$result);
				  return $strparam;
			}
		}
	}

}

?>